package com.ruoyi.qsng.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.SnowFlakeUtil;
import com.ruoyi.qsng.domain.KClearinghouse;
import com.ruoyi.qsng.domain.KClearinghouseFocus;
import com.ruoyi.qsng.mapper.KClearinghouseCommentMapper;
import com.ruoyi.qsng.mapper.KClearinghouseFocusMapper;
import com.ruoyi.qsng.mapper.KClearinghouseLikeMapper;
import com.ruoyi.qsng.mapper.KClearinghouseMapper;
import com.ruoyi.qsng.service.IKClearinghouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 交流中心Service业务层处理
 *
 * @author kanglou
 * @date 2023-06-07
 */
@Service
public class KClearinghouseServiceImpl implements IKClearinghouseService {
    @Autowired
    private KClearinghouseMapper kClearinghouseMapper;

    @Autowired
    private KClearinghouseLikeMapper kClearinghouseLikeMapper;

    @Autowired
    private KClearinghouseFocusMapper kClearinghouseFocusMapper;

    @Autowired
    private KClearinghouseCommentMapper kClearinghouseCommentMapper;

    /**
     * 查询交流中心
     *
     * @param id 交流中心主键
     * @return 交流中心
     */
    @Override
    public KClearinghouse selectKClearinghouseById(String id) {
        return kClearinghouseMapper.selectKClearinghouseById(id);
    }

    @Override
    public KClearinghouse selectKClearinghouseByIdForApp(String id) {
        KClearinghouse kClearinghouse = kClearinghouseMapper.selectKClearinghouseByIdForApp(id);
        //是否关注
        KClearinghouseFocus kClearinghouseFocus = new KClearinghouseFocus();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();

        kClearinghouseFocus.setUserId(kClearinghouse.getCreateBy());
        kClearinghouseFocus.setCreateBy(String.valueOf(userId));
//        kClearinghouseFocus.
        List<KClearinghouseFocus> kClearinghouseFocusList = kClearinghouseFocusMapper.selectKClearinghouseFocusList(kClearinghouseFocus);
        if (kClearinghouseFocusList.size() > 0) {
            kClearinghouse.setIsFollowed(1);
        } else {
            kClearinghouse.setIsFollowed(0);
        }
        return kClearinghouse;
    }

    /**
     * 查询交流中心列表
     *
     * @param kClearinghouse 交流中心
     * @return 交流中心
     */
    @Override
    public List<KClearinghouse> selectKClearinghouseList(KClearinghouse kClearinghouse) {
        return kClearinghouseMapper.selectKClearinghouseList(kClearinghouse);
    }

    @Override
    public List<KClearinghouse> selectKClearinghouseListForAPP(KClearinghouse kClearinghouse) {
        return kClearinghouseMapper.selectKClearinghouseListForApp(kClearinghouse);
    }

    /**
     * 新增交流中心
     *
     * @param kClearinghouse 交流中心
     * @return 结果
     */
    @Override
    public int insertKClearinghouse(KClearinghouse kClearinghouse) {
        kClearinghouse.setId(SnowFlakeUtil.getNextId());
        kClearinghouse.setCreateTime(DateUtils.getNowDate());
        return kClearinghouseMapper.insertKClearinghouse(kClearinghouse);
    }

    /**
     * 修改交流中心
     *
     * @param kClearinghouse 交流中心
     * @return 结果
     */
    @Override
    public int updateKClearinghouse(KClearinghouse kClearinghouse) {
        kClearinghouse.setUpdateTime(DateUtils.getNowDate());
        return kClearinghouseMapper.updateKClearinghouse(kClearinghouse);
    }

    /**
     * 批量删除交流中心
     *
     * @param ids 需要删除的交流中心主键
     * @return 结果
     */
    @Override
    public int deleteKClearinghouseByIds(String[] ids) {
        return kClearinghouseMapper.deleteKClearinghouseByIds(ids);
    }

    /**
     * 删除交流中心信息
     *
     * @param id 交流中心主键
     * @return 结果
     */
    @Override
    public int deleteKClearinghouseById(String id) {
        return kClearinghouseMapper.deleteKClearinghouseById(id);
    }
}
